package com.bazhua.bazo.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bazhua.bazo.repository.entity.BannerInfo;
import com.bazhua.bazo.repository.entity.Coupon;
import com.bazhua.bazo.repository.entity.CouponRecord;
import com.bazhua.bazo.repository.mapper.BannerInfoMapper;
import com.bazhua.bazo.repository.mapper.CouponRecordMapper;
import com.bazhua.bazo.service.ICouponRecordService;
import com.sun.org.apache.bcel.internal.generic.RET;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: starmile
 * @Description:
 * @Create: 2021-01-26 0:43
 **/
@Service
public class CouponRecordServiceImpl extends ServiceImpl<CouponRecordMapper, CouponRecord> implements ICouponRecordService {

    public Integer getCouponRecordCount(Long couponId, Integer status, Long userId) {
        QueryWrapper<CouponRecord> wrapper = new QueryWrapper();
        wrapper.eq("coupon_id", couponId);
        if (status != null) {
            wrapper.eq("status", status);
        }
        if (userId != null) {
            wrapper.eq("user_id", userId);
        }
        return baseMapper.selectCount(wrapper);
    }

    public CouponRecord getCouponRecordByCouponId(Long couponId, Integer status, Long userId) {
        QueryWrapper<CouponRecord> wrapper = new QueryWrapper();
        wrapper.eq("coupon_id", couponId);
        if (status != null) {
            wrapper.eq("status", status);
        }
        if (userId != null) {
            wrapper.eq("user_id", userId);
        }
        return baseMapper.selectOne(wrapper);
    }

    public CouponRecord getCouponRecordByNumber(String couponNumber) {
        QueryWrapper<CouponRecord> wrapper = new QueryWrapper();
        wrapper.eq("coupon_number", couponNumber);
        CouponRecord couponRecord = baseMapper.selectOne(wrapper);
        return couponRecord;
    }

    public CouponRecord checkCouponRecord(String couponNumber, Long userId) {
        CouponRecord couponRecord = getCouponRecordByNumber(couponNumber);
        couponRecord.setStatus(1);
        couponRecord.setCheckUserId(userId);
        baseMapper.updateById(couponRecord);
        return couponRecord;
    }

    public void saveCouponRecord(CouponRecord couponRecord) {
        if (couponRecord.getId() != null && couponRecord.getId() > 0) {
            baseMapper.updateById(couponRecord);
            return;
        }
        baseMapper.insert(couponRecord);
    }

    public Page<CouponRecord> getCouponRecordPage(Page<CouponRecord> page, QueryWrapper<CouponRecord> queryWrapper) {
        page = baseMapper.selectPage(page, queryWrapper);
        return page;
    }

    public CouponRecord getCouponRecord(Long couponRecordId) {
        CouponRecord couponRecord = baseMapper.selectById(couponRecordId);
        return couponRecord;
    }
}
